<template>
  <div class="content">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="query"
        class="demo-form-inline"
      >
        <el-form-item label="部门">
          <el-select v-model="query.department_id">
            <el-option label="所有部门" :value="0"></el-option>
            <el-option
              v-for="item in departmentOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位">
          <el-select v-model="query.station_id">
            <el-option label="所有岗位" :value="0"></el-option>
            <el-option
              v-for="item in stationOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getTableInfo"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe>
      <el-table-column type="index" label="#" fixed="left" />
      <el-table-column prop="department.title" label="部门" />
      <el-table-column prop="title" label="岗位" />
      <el-table-column prop="trainerUser.real_name" label="培训员" />
      <el-table-column prop="trainerUser.mobile" label="联系方式" />
      <el-table-column prop="student_count" label="学员数量" />
      <el-table-column label="操作" fixed="right" width="300">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleFile(scope.row.id, scope.row.department.id)"
          >
            培训材料
          </el-button>
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleStudent(scope.row.id)"
            v-auth="'train_edit'"
          >
            学员列表
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Drawer
      ref="studentRef"
      @getTable="getTableInfo"
      :department="departmentOption"
    />
    <LogDrawer ref="logDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import { getDepartmentOptionsApi } from "@/api/company/department";
import Drawer from "./component/studentDraw.vue";
import LogDrawer from "./component/dialogLog.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getListAPi } from "@/api/company/station";
import { trainStationApi } from "@/api/company/train";
const studentRef = ref<any>(null);
const logDrawerRef = ref<any>(null);
const departmentOption = ref<any[]>([]);
const stationOption = ref<any[]>([]);
const query = reactive({
  department_id: 0,
  station_id: 0,
});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(
  () => query.department_id,
  (newVal) => {
    if (newVal) {
      getStationOptions();
    } else {
      stationOption.value = [];
    }
  }
);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
  getDepartmentOptionsApi().then((res) => {
    res.data.forEach((el) => {
      departmentOption.value.push({
        id: el.id,
        title: el.title,
      });
      if (el.children && el.children.length) {
        el.children.forEach((el) => {
          departmentOption.value.push({
            id: el.id,
            title: el.title,
          });
        });
      }
    });
  });
  getTableInfo();
});
const getStationOptions = () => {
  getListAPi({
    department_id: query.department_id,
  }).then((res) => {
    stationOption.value = res.data;
  });
};
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};

function getTableInfo() {
  trainStationApi({
    ...query,
  }).then((res) => {
    tableData.value = res.data;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此条面试信息吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

const handleStudent = (id?: number) => {
  studentRef.value.dialogOpen(id);
};
const handleFile = (id: number, department_id: number) => {
  logDrawerRef.value.dialogOpen(id, department_id);
};
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
